//----------------------------------------
//Variables
//----------------------------------------

var oldSelectEC, oldSelectType, oldSelectEnseignant, oldSelectSalle, oldSelectJour, oldHeureDebut, oldMinuteDebut, oldHeureFin, oldMinuteFin,oldSelectPromotion;
var isInterval = false;
//---------------------------------------
//Events
//---------------------------------------

function page_load(){
    displayPromos();
    getOldsValues();
}

//---------------------------------------

function selectIndexChanged(id){
    document.getElementById(id).setAttribute('style', 'background-color : '+(document.getElementById(id).value == '' ? 'red' : 'white'));
}

//---------------------------------------
//Functions
//---------------------------------------

function getOldsValues(){
    this.oldSelectEC  = document.getElementById('selectEC').value;
    this.oldSelectType  = document.getElementById('selectType').value;
    this.oldSelectEnseignant  = document.getElementById('selectEnseignant').value;
    this.oldSelectSalle  = document.getElementById('selectSalle').value;
    this.oldSelectJour  = document.getElementById('date').value;

    this.oldHeureDebut  = document.getElementById('heureDebut').value;
    this.oldMinuteDebut  = document.getElementById('minuteDebut').value;

    this.oldHeureFin  = document.getElementById('heureFin').value;
    this.oldMinuteFin  = document.getElementById('minuteFin').value;

    this.oldSelectPromotion = document.getElementById('selectedPromotion').value;
}

//---------------------------------------

function disableAll(){
    var f = document.getElementsByTagName('input');
    for(var i=0;i<f.length;i++){
        if(f[i].getAttribute('type')=='submit'
            || f[i].getAttribute('type')=='select'){
            f[i].setAttribute('disabled',true)
        }
    }
    document.getElementById('btnValider').style.visibility = 'hidden';
    document.getElementById('btnAnnuler').style.visibility = 'hidden';
    document.getElementById('loadingGIF').style.visibility = 'visible';
}

//---------------------------------------

function enableAll(){
    var f = document.getElementsByTagName('input');
    for(var i=0;i<f.length;i++){
        if(f[i].getAttribute('type')=='submit'
            || f[i].getAttribute('type')=='select'){
            f[i].setAttribute('disabled',true)
        }
    }
    document.getElementById('btnValider').style.visibility = 'visible';
    document.getElementById('btnAnnuler').style.visibility = 'visible';
    document.getElementById('loadingGIF').style.visibility = 'hidden';
}

//---------------------------------------

function displayPromos(){

    var idFormation = (document.getElementById('selectFormation') != null && document.getElementById('selectFormation').value != "") ? document.getElementById('selectFormation').value : 0 ;
    var selectedPromotion = (document.getElementById('selectedPromotion') != null && document.getElementById('selectedPromotion').value != "") ? document.getElementById('selectPromotion').value : 0;

    var url = './PromotionSupplier';

    var parameters = 'formation='+idFormation;
    parameters += '&selectedPromotion='+selectedPromotion;

    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', url, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            document.getElementById('promotion').innerHTML = self.xmlHttpReq.responseText;
        }
    }
    self.xmlHttpReq.send(parameters);
}

//---------------------------------------

function submitForm(){

    document.getElementById('alert').innerHTML = '';
    document.getElementById('responseUpdate').innerHTML = '';

    disableAll();

    var selectEC  = document.getElementById('selectEC').value;
    var selectType  = document.getElementById('selectType').value;
    var selectEnseignant  = document.getElementById('selectEnseignant').value;
    var selectSalle  = document.getElementById('selectSalle').value;
    var selectJour  = document.getElementById('date').value;
    var dateDebut = document.getElementById('dateDebut').value;
    var dateFin = document.getElementById('dateFin').value;
    var selectWeekDay = document.getElementById('selectWeekDay').value;
    
    var heureDebut  = document.getElementById('heureDebut').value;
    var minuteDebut  = document.getElementById('minuteDebut').value;

    var heureFin  = document.getElementById('heureFin').value;
    var minuteFin  = document.getElementById('minuteFin').value;

    var selectPromotion = document.getElementById('selectPromotion').value;

    var isOk = true;

    if(selectEC==''){
        document.getElementById('selectEC').setAttribute('style', 'background-color : red');
        isOk = false;
    }
    if( selectType == ''){
        document.getElementById('selectType').setAttribute('style', 'background-color : red');
        isOk = false;
    }
    if(selectEnseignant==''){
        document.getElementById('selectEnseignant').setAttribute('style', 'background-color : red');
        isOk = false;
    }
    if(selectJour==''){
        document.getElementById('date').setAttribute('style', 'background-color : red');
        isOk = false;
    }
    if(selectSalle== ''){
        document.getElementById('selectSalle').setAttribute('style', 'background-color : red');
        isOk = false;
    }

    if(selectPromotion == ''){
        if(document.getElementById('selectPromotion') != null)
            document.getElementById('selectPromotion').setAttribute('style', 'background-color : red');
        isOk = false;
    }

    if(isInterval == true){
        if(dateDebut == ''){
            document.getElementById('dateDebut').setAttribute('style', 'background-color : red');
            isOk = false;
        }
        if(dateFin == ''){
            document.getElementById('dateFin').setAttribute('style', 'background-color : red');
            isOk = false;
        }
        if(selectWeekDay == ''){
            document.getElementById('selectWeekDay').setAttribute('style', 'background-color : red');
            isOk = false;
        }
    }

    if(isInterval == false && selectJour==''){
        document.getElementById('date').setAttribute('style', 'background-color : red');
        isOk = false;
    }

    if(isOk == false){
        document.getElementById('alert').innerHTML = '<p> <font color="red"> Veuillez renseigner tous les champs </font> </p>';
        enableAll();
        return;
    }

    if(((parseInt(heureDebut) * 60) + parseInt(minuteDebut)) >= (parseInt(heureFin * 60) + parseInt(minuteFin))){
        document.getElementById('alert').innerHTML = '<p> <font color="red"> L\'heure de début ne peut être égale ou supérieure à la date de début</font> </p>';
        enableAll();
        return;
    }

    if(isInterval == true){
        var tmpDateDebut = dateDebut.split('/', dateDebut.length);
        var tmpDateFin = dateFin.split('/', dateFin.length);

        var oDateDebut = new Date(tmpDateDebut[2], tmpDateDebut[1], tmpDateDebut[0]);
        var oDateFin= new Date(tmpDateFin[2], tmpDateFin[1], tmpDateFin[0]);

        if(oDateDebut >= oDateFin){
            document.getElementById('alert').innerHTML = '<p> <font color="red"> La date de début ne peut être supérieure ou égale à la date de début</font> </p>';
            enableAll();
            return;
        }
    }

    var url = './modifCrenau';
    var parameters = 'ec='+selectEC;
    parameters += '&type='+selectType;
    parameters += '&salle='+selectSalle;
    parameters += '&jour='+selectJour;
    parameters += '&dateDebut='+dateDebut;
    parameters += '&dateFin='+dateFin;
    parameters += '&enseignant='+selectEnseignant;
    parameters += '&heureDebut='+heureDebut;
    parameters += '&minuteDebut='+minuteDebut;
    parameters += '&heureFin='+heureFin;
    parameters += '&minuteFin='+minuteFin;
    parameters += '&promotion='+selectPromotion;
    parameters += '&oldEc='+oldSelectEC;
    parameters += '&oldType='+oldSelectType;
    parameters += '&oldJour='+oldSelectJour;
    parameters += '&oldSalle='+oldSelectSalle;
    parameters += '&oldEnseignant='+oldSelectEnseignant;
    parameters += '&oldHeureDebut='+oldHeureDebut;
    parameters += '&oldMinuteDebut='+oldMinuteDebut;
    parameters += '&oldHeureFin='+oldHeureFin;
    parameters += '&oldMinuteFin='+oldMinuteFin;
    parameters += '&oldPromotion='+oldSelectPromotion;
    parameters += '&selectWeekDay='+selectWeekDay;
    parameters += '&isInterval='+isInterval;

    //alert('Parameters : '+parameters);

    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', url, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            enableAll();
            document.getElementById('responseUpdate').innerHTML = self.xmlHttpReq.responseText;
        }
    }
    self.xmlHttpReq.send(parameters);
    document.getElementById('selectedPromotion').value = selectPromotion;
    getOldsValues();
}

//---------------------------------------

function showIntervalSelect(){
    document.getElementById("jour").style.display = 'none';
    document.getElementById("intervalJour").style.display = 'block';
    isInterval = true;
}

//---------------------------------------

function showDaySelect(){
    document.getElementById("jour").style.display = 'block';
    document.getElementById("intervalJour").style.display = 'none';
    isInterval = false;
}

//---------------------------------------